package client;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedHashMap;

enum Status
{
    loginRoom, gameRoom
};

public class Model implements IModel
{

    String name = null;
    Status status = Status.loginRoom;
    String targetURL = "http://localhost:8001";
    HttpURLConnection connection = null;
    URL url;
    LinkedHashMap<String, Integer> playerList = new LinkedHashMap<>();

    

    /**
     * @param name
     * @return true if success; false if login is already in use
     */
    boolean login(String name)
    {
        Boolean ret = true;
        String str = null;
        String message = "login^" + name;
        str = this.send(message);
        if (str.split("\\^")[1].equals("NO"))
        {
            ret = false;
        } else
        {
            this.name = name;
            String lst = str.split("\\^")[2];
            parsePlayerList(lst);
        }
        return ret;
    }

    private void parsePlayerList(String lst)
    {
        //nick:0;nick1:1;nick2:0;
        String s[]=lst.split(";");
        for(String p:s)
        {
            String name = p.split(":")[0];
            int status = Integer.parseInt(p.split(":")[1]);
            playerList.put(name, status);
        }
    }

    public String send(String info)
    {
        sendInfo(info);
        return getResponse();
    }

    /**
     * Sends information to server.
     */
    public void sendInfo(String myInfo)
    {
        createConnection();
        try
        {
            DataOutputStream wr;
            wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(myInfo);
            wr.flush();
            wr.close();
        } catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }

    /**
     * Reads response from server. Should be called instantly after sendInfo()
     * method.
     */
    public String getResponse()
    {
        String ret = null;
        try
        {
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuilder response = new StringBuilder();
            if ((line = rd.readLine()) != null)
            {
                response.append(line);
            }
            rd.close();
            ret = response.toString();
        } catch (IOException ex)
        {
            ex.printStackTrace();
        }
        return ret;
    }

    private void createConnection()
    {
        try
        {
            url = new URL(targetURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "text/html");
            connection.setRequestProperty("Content-Language", "en-US");
            connection.setAllowUserInteraction(true);
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
        } catch (IOException ex)
        {
            ex.printStackTrace();
        }

    }
}
